﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LibEBML.Common
{
    [Obsolete]
    public class Cue : MatroskaBase
    {
        public Cue(EBMLContainer container)
            : base(container)
        {

        }
        protected override void Initialize()
        {
            //var ts = container[0xb7].FirstOrDefault() as EBMLContainer;
            //if (ts != null)
            //{
            //    CueTrackPositions = ts.Select(mt => new CueTrackPosition(mt as EBMLContainer)).ToList();
            //}
        }
        protected override void InitDefaultValues()
        {
        }

        public virtual IList<CuePoint> Points { get; protected set; }
    }

    public class CuePoint : MatroskaBase
    {
        public CuePoint(EBMLContainer container)
            : base(container)
        {

        }
        protected override void Initialize()
        {
            var tc = container[0xb3].FirstOrDefault() as EBMLType<ulong>;
            if (tc != null)
            {
                CueTime = (long)tc.Value;
            }
            CueTrackPositions = container[0xb7].Select(mt => new CueTrackPosition(mt as EBMLContainer)).ToList();
        }
        protected override void InitDefaultValues()
        {
            CueTime = 0;
        }
        public long CueTime { get; private set; }
        public IList<CueTrackPosition> CueTrackPositions { get; set; }
    }
    public class CueTrackPosition : MatroskaBase
    {
        public int CueTrack { get; set; }
        public long ClusterPosition { get; set; }
        public long BlockNumber { get; set; }
        public int CodeState { get; set; }
        public CueReference Reference { get; set; }

        protected override void Initialize()
        {
            var tc = container[0xF7].FirstOrDefault() as EBMLType<ulong>;
            if (tc != null)
            {
                CueTrack = (int)tc.Value;
            }
            tc = container[0xf1].FirstOrDefault() as EBMLType<ulong>;
            if (tc != null)
            {
                ClusterPosition = (long)tc.Value;
            }
            tc = container[0x5378].FirstOrDefault() as EBMLType<ulong>;
            if (tc != null)
            {
                BlockNumber = (long)tc.Value;
            }
            tc = container[0xea].FirstOrDefault() as EBMLType<ulong>;
            if (tc != null)
            {
                CodeState = (int)tc.Value;
            }
            var rf = container[0xdb].FirstOrDefault() as EBMLContainer;
            if (rf != null)
            {
                Reference = new CueReference(rf);
            }
        }

        protected override void InitDefaultValues()
        {
            BlockNumber = 1;
            CodeState = 0;
        }
        public CueTrackPosition(EBMLContainer container)
            : base(container)
        {

        }
    }
    public class CueReference : MatroskaBase
    {
        public long CueRefTime { get; set; }
        public long CueRefCluster { get; set; }
        public int CueRefNumber { get; set; }
        public int CueRefCodecState { get; set; }

        protected override void Initialize()
        {
            var tc = container[0x96].FirstOrDefault() as EBMLType<ulong>;
            if (tc != null)
            {
                CueRefTime = (long)tc.Value;
            }
            tc = container[0x97].FirstOrDefault() as EBMLType<ulong>;
            if (tc != null)
            {
                CueRefCluster = (long)tc.Value;
            }
            tc = container[0x535f].FirstOrDefault() as EBMLType<ulong>;
            if (tc != null)
            {
                CueRefNumber = (int)tc.Value;
            }
            tc = container[0xeb].FirstOrDefault() as EBMLType<ulong>;
            if (tc != null)
            {
                CueRefCodecState = (int)tc.Value;
            }
        }

        protected override void InitDefaultValues()
        {
            CueRefNumber = 1;
            CueRefCodecState = 0;
        }
        public CueReference(EBMLContainer container)
            : base(container)
        {

        }
    }
}
